package com.ruoyi.eamp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampEquipmentMy;
import com.ruoyi.eamp.domain.EampEquipmentUse;
import com.ruoyi.eamp.mapper.EampEquipmentMyMapper;
import com.ruoyi.eamp.mapper.EampEquipmentUseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eamp.mapper.EampEquipmentMapper;
import com.ruoyi.eamp.domain.EampEquipment;
import com.ruoyi.eamp.service.IEampEquipmentService;

/**
 * 仪器设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampEquipmentServiceImpl implements IEampEquipmentService 
{
    @Autowired
    private EampEquipmentMapper eampEquipmentMapper;
    @Autowired
    private EampEquipmentMyMapper eampEquipmentMyMapper;

    @Autowired
    private EampEquipmentUseMapper eampEquipmentUseMapper;

    /**
     * 查询仪器设备
     * 
     * @param equipmentaId 仪器设备主键
     * @return 仪器设备
     */
    @Override
    public EampEquipment selectEampEquipmentByEquipmentaId(Long equipmentaId)
    {
        return eampEquipmentMapper.selectEampEquipmentByEquipmentaId(equipmentaId);
    }

    /**
     * 查询仪器设备列表
     * 
     * @param eampEquipment 仪器设备
     * @return 仪器设备
     */
    @Override
    public List<EampEquipment> selectEampEquipmentList(EampEquipment eampEquipment)
    {
        return eampEquipmentMapper.selectEampEquipmentList(eampEquipment);
    }

    /**
     * 新增仪器设备
     * 
     * @param eampEquipment 仪器设备
     * @return 结果
     */
    @Override
    public int insertEampEquipment(EampEquipment eampEquipment)
    {
       int i =  eampEquipmentMapper.insertEampEquipment(eampEquipment);

        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        eampEquipmentMapper.updateEampEquipment(eampEquipment);
        EampEquipmentUse eampEquipmentUse= new EampEquipmentUse();
        eampEquipmentUse.setEquipmentId(eampEquipment.getEquipmentaId());
        eampEquipmentUse.setCreateDate(new Date());
        eampEquipmentUse.setCreateBy(userName);
        eampEquipmentUse.setUsestatus(4L);//新增
            eampEquipmentUseMapper.insertEampEquipmentUse(eampEquipmentUse);

        return i;
    }

    /**
     * 修改仪器设备
     * 
     * @param eampEquipment 仪器设备
     * @return 结果
     */
    @Override
    public int updateEampEquipment(EampEquipment eampEquipment)
    {  String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        int i = eampEquipmentMapper.updateEampEquipment(eampEquipment);
        EampEquipmentUse eampEquipmentUse= new EampEquipmentUse();
        eampEquipmentUse.setEquipmentId(eampEquipment.getEquipmentaId());
        eampEquipmentUse.setCreateDate(new Date());
        eampEquipmentUse.setCreateBy(userName);
        eampEquipmentUse.setUsestatus(3L);//修改
        eampEquipmentUseMapper.insertEampEquipmentUse(eampEquipmentUse);

        return eampEquipmentMapper.updateEampEquipment(eampEquipment);
    }

    /**
     * 批量删除仪器设备
     * 
     * @param equipmentaIds 需要删除的仪器设备主键
     * @return 结果
     */
    @Override
    public int deleteEampEquipmentByEquipmentaIds(Long[] equipmentaIds)
    {
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();

//        List<EampEquipmentUse> list=new ArrayList<>();
        for (Long equipmentaId:equipmentaIds) {
            EampEquipmentUse eampEquipmentUse= new EampEquipmentUse();
            eampEquipmentUse.setEquipmentId(equipmentaId);
            eampEquipmentUse.setCreateDate(new Date());
            eampEquipmentUse.setCreateBy(userName);
            eampEquipmentUse.setUsestatus(2L);//假删除
//            list.add(eampEquipmentUse);
            eampEquipmentUseMapper.insertEampEquipmentUse(eampEquipmentUse);
        }

//      eampEquipmentUseMapper.insertEampEquipmentUse(list);
//     eampEquipmentUseMapper.insertEampEquipmentUseAndDelete(list);


        return eampEquipmentMapper.deleteEampEquipmentByEquipmentaIds(equipmentaIds);
    }

    /**
     * 删除仪器设备信息
     * 
     * @param equipmentaId 仪器设备主键
     * @return 结果
     */
    @Override
    public int deleteEampEquipmentByEquipmentaId(Long equipmentaId)
    {


        String userName = SecurityUtils.getLoginUser().getUser().getUserName();

//        EampEquipment eampEquipment =new EampEquipment();
//        int i2 = eampEquipmentMapper.updateEampEquipment(eampEquipment);

        EampEquipmentUse eampEquipmentUse= new EampEquipmentUse();
        eampEquipmentUse.setEquipmentId(equipmentaId);
        eampEquipmentUse.setCreateDate(new Date());
        eampEquipmentUse.setCreateBy(userName);
        eampEquipmentUse.setUsestatus(2L);//假删除
        eampEquipmentUseMapper.insertEampEquipmentUse(eampEquipmentUse);
        int i = eampEquipmentMapper.deleteEampEquipmentByEquipmentaId(equipmentaId);
        return i;

    }

    @Override
    public int outable(EampEquipment eampEquipment) {
        //更新仪器状态
        int i = eampEquipmentMapper.updateEampEquipment(eampEquipment);
   //我的使用新增
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        EampEquipmentMy eampEquipmentMy =new EampEquipmentMy();
        eampEquipmentMy.setEquipmentId(eampEquipment.getEquipmentaId());
        eampEquipmentMy.setCreateTime(new Date());
        eampEquipmentMy.setCreateBy(userName);
        eampEquipmentMy.setUsestatus(eampEquipment.getUsestatus());
         eampEquipmentMyMapper.insertEampEquipmentMy(eampEquipmentMy);

//使用记录新增

        EampEquipmentUse eampEquipmentUse= new EampEquipmentUse();
        eampEquipmentUse.setEquipmentId(eampEquipment.getEquipmentaId());
        eampEquipmentUse.setCreateDate(new Date());
        eampEquipmentUse.setCreateBy(userName);
        eampEquipmentUse.setUsestatus(0L);
        eampEquipmentUseMapper.insertEampEquipmentUse(eampEquipmentUse);


        return i;
    }
}
